﻿using System;
using System.Data;
using UniMana.Data;  using UniMana.Utils.Helper;

using UniMana.Utils.Data;
using UniMana.Utils.Controls;
using System.Windows.Forms;
namespace UniMana.Win.Leadership
{
    public partial class FrmReportClass : DevExpress.XtraEditors.XtraForm
    {        
        #region [Khai báo biến toàn cục]
        private DBMain m_ReportStudent_Course = new DBMain();
        private DBMain m_data = new DBMain();
        private string Course = " Khóa ";
        private string Department = " Khoa ";
        private string Levels = " Bậc ";
        private string Majors = " Ngành ";
        private string TrainingTypes = " Loại Hình ";
        private int m_iDpt = 0, m_iLel = 0, m_iMaj = 0, m_iTty = 0, m_iCou = 0;
        DataTable dt = new DataTable();
        private int _Flag = 1;
        #endregion
        #region [Load dữ liệu]
        private void FrmReportClass_Load(object sender, EventArgs e)
        {
            LoadData_Lue();
        }
        public FrmReportClass()
        {
            InitializeComponent();
        }
        #endregion
        #region [Sự kiện các nút]
        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {            
            #region
            try
            {
                if (_Flag == 1)
                {
                    CheckSelect();
                    if (m_iDpt == 0 && lueCatalogue_Department.GetIntValue() >= 0)
                    {
                        m_iDpt = Math.Max(m_iLel, Math.Max(m_iMaj, Math.Max(m_iTty, m_iCou))) + 1;
                    }
                    if (ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Department.EditValue) <= 0)
                    {
                        lueCatalogue_Level.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetLevelInTrainingClass(
                            this.m_data.m_DCReadOnly.Catalogue_Levels)).ToDataTable();
                        lueCatalogue_Level.GetData(dt, noChoose: true);
                        lueCatalogue_Major.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetMajorInTrainingClass(
                            this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                        lueCatalogue_Major.GetData(dt, noChoose: true);
                        lueCatalogue_TrainingType.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetTrainingTypeInTrainingClass(
                            this.m_data.m_DCReadOnly.Catalogue_TrainingTypes)).ToDataTable();
                        lueCatalogue_TrainingType.GetData(dt, "Catalogue_TrainingType", noChoose: true);
                        lueCatalogue_Course.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetCourseInTrainingClass(
                            this.m_data.m_DCReadOnly.Catalogue_Courses)).ToDataTable();
                        lueCatalogue_Course.GetData(dt, noChoose: true);
                    }
                    else
                    {                        
                        // Lấy dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học theo khoa                        
                        lueCatalogue_Level.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetLevelByDepartment(
                            this.m_data.m_DCReadOnly.Catalogue_Levels, 
                            ReportClassData.ConvertValueLookUpEditToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Level.GetData(dt, noChoose: true);
                        lueCatalogue_Major.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetMajorByDepartment(
                        this.m_data.m_DCReadOnly.Catalogue_Majors, 
                        ReportClassData.ConvertValueLookUpEditToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Major.GetData(dt, noChoose: true);
                        lueCatalogue_TrainingType.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetTrainingTypeByDepartment(
                            this.m_data.m_DCReadOnly.Catalogue_TrainingTypes, 
                            ReportClassData.ConvertValueLookUpEditToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_TrainingType.GetData(dt, "Catalogue_TrainingType", noChoose: true);
                        lueCatalogue_Course.EditValue = null;
                        dt = 
                            (this.m_data.m_DCReadOnly.Training_Classes.GetCourseByDepartment(
                            this.m_data.m_DCReadOnly.Catalogue_Courses, 
                            ReportClassData.ConvertValueLookUpEditToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Course.GetData(dt, noChoose: true);
                    }
                }
            }
            catch { }
            #endregion
        }
        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {            
            #region
            try
            {
                CheckSelect();
                if (m_iLel == 0 && lueCatalogue_Level.GetIntValue() >= 0)
                {
                    m_iLel = Math.Max(m_iDpt, Math.Max(m_iMaj, Math.Max(m_iTty, m_iCou))) + 1;
                }
                if (ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Level.EditValue) > 0)
                {
                    if (ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Department.EditValue) > 0)
                    {
                        dt = (this.m_data.m_DCReadOnly.Training_Classes.GetMajorKeyByDepartmentLevel(ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Department.EditValue), ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Level.EditValue))).ToDataTable();
                        lueCatalogue_Major.EditValue = dt.Rows[0][0];
                    }
                }
            }
            catch { }
            #endregion
        }
        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {           
            #region
            try
            {
                CheckSelect();
                if (m_iMaj == 0 && lueCatalogue_Major.GetIntValue() >= 0)
                {
                    m_iMaj = Math.Max(m_iDpt, Math.Max(m_iLel, Math.Max(m_iTty, m_iCou))) + 1;
                }
                if (ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Major.EditValue) > 0)
                {
                    dt = (this.m_data.m_DCReadOnly.Training_Classes.GetDepartmentKeyByMajor(ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Major.EditValue))).ToDataTable();
                    _Flag = 0;
                    lueCatalogue_Department.EditValue = dt.Rows[0][0];
                    _Flag = 1;
                    dt = (this.m_data.m_DCReadOnly.Training_Classes.GetLevelKeyByDepartmentMajor(ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Department.EditValue), ReportClassData.ConvertValueLookUpEditToInt(lueCatalogue_Major.EditValue))).ToDataTable();
                    lueCatalogue_Level.EditValue = dt.Rows[0][0];
                }
                else
                {
                    // no action
                }
            }
            catch
            {
            }
            #endregion
        }        
        private void lueCatalogue_TrainingType_EditValueChanged(object sender, EventArgs e)
        {
            CheckSelect();
            if (m_iTty == 0 && lueCatalogue_TrainingType.GetIntValue() >= 0)
            {
                m_iTty = Math.Max(m_iDpt, Math.Max(m_iMaj, Math.Max(m_iLel, m_iCou))) + 1;
            }
        }
        private void lueCatalogue_Course_EditValueChanged(object sender, EventArgs e)
        {
            CheckSelect();
            if (m_iCou == 0 && lueCatalogue_Course.GetIntValue() >= 0)
            {
                m_iCou = Math.Max(m_iTty, Math.Max(m_iLel, Math.Max(m_iDpt, m_iMaj))) + 1;
            }
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            #region [Xét Thông tin]
            CheckSelect();
            grcClass.DataSource = null;
            cacStudent_Course.DataSource = null;
            if (m_iCou == 0 && m_iDpt == 0 && m_iLel == 0 && m_iMaj == 0 && m_iTty == 0)
            {
                m_iDpt = 1;
                m_iLel = 2;
                m_iMaj = 3;
                m_iTty = 4;
                m_iCou = 5;
            }
            #endregion
            #region
            DataTable dt =
                m_ReportStudent_Course.m_DCReadOnly.Training_Classes.GetReport_Class(
                    m_ReportStudent_Course.m_DCReadOnly.Catalogue_Courses,
                    m_ReportStudent_Course.m_DCReadOnly.Catalogue_Departments,
                    m_ReportStudent_Course.m_DCReadOnly.Catalogue_Levels,
                    m_ReportStudent_Course.m_DCReadOnly.Catalogue_Majors,
                    m_ReportStudent_Course.m_DCReadOnly.Catalogue_TrainingTypes,
                    lueCatalogue_Course.GetIntValue(),
                    lueCatalogue_Department.GetIntValue(),
                    lueCatalogue_Level.GetIntValue(),
                    lueCatalogue_Major.GetIntValue(),
                    lueCatalogue_TrainingType.GetIntValue(),
                    m_iDpt,
                    m_iLel,
                    m_iMaj,
                    m_iTty,
                    m_iCou).ToDataTable();
            #endregion
            if (dt.Rows.Count >= 0)
            {
                #region [lấy Dữ Liệu]
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Columns[i].ColumnName == "Catalogue_DepartmentName")
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            dt.Rows[j][i] = Department + dt.Rows[j][i].ToString();
                        }
                    }
                    if (dt.Columns[i].ColumnName == "Catalogue_LevelName")
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            dt.Rows[j][i] = Levels + dt.Rows[j][i].ToString();
                        }
                    }
                    if (dt.Columns[i].ColumnName == "Catalogue_MajorName")
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            dt.Rows[j][i] = Majors + dt.Rows[j][i].ToString();
                        }
                    }
                    if (dt.Columns[i].ColumnName == "Catalogue_TrainingTypeName")
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            dt.Rows[j][i] = TrainingTypes + dt.Rows[j][i].ToString();
                        }
                    }
                    if (dt.Columns[i].ColumnName == "Catalogue_CourseName")
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            dt.Rows[j][i] = Course + dt.Rows[j][i].ToString();
                        }
                    }
                }
                #endregion
                #region [Xử Lý Dữ Liệu]
                ArrangeColume(dt, m_iDpt, m_iLel, m_iMaj, m_iTty, m_iCou);
                #endregion
                #region [Xuất Dữ Liệu]
                dt.Columns.Add("Name");
                dt.Columns.Add("STT");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i][dt.Columns.Count - 2] = "";
                    for (int j = 0; j < dt.Columns.Count - 3; j++)
                    {
                        dt.Rows[i][dt.Columns.Count - 2] += dt.Rows[i][j].ToString();
                    }
                    dt.Rows[i][dt.Columns.Count - 1] = i + 1;
                }
                #endregion
                #region[Xuất Ra Lưới Và Biểu Đồ]
                grcClass.DataSource = dt;
                cacStudent_Course.DataSource = dt;
                cacStudent_Course.Series[0].ArgumentDataMember = "STT";
                cacStudent_Course.Series[0].TopNOptions.Enabled = true;
                cacStudent_Course.Series[0].Name = "Thứ Tự";
                cacStudent_Course.Series[0].TopNOptions.Enabled = true;
                cacStudent_Course.Series[0].ValueDataMembersSerializable = "Total";
                #endregion
            }
            else
            {
                MessageBox.Show("Không Có Lớp Nào Thỏa Điều Kiện", "Thông Báo");
            }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            compositeLinkHienThi.ClearDocument();
            //this.compositeLinkHienThi.RtfReportHeader = this.compositeLinkHienThi.RtfReportHeader.Replace("tenlop", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(ten));
            compositeLinkHienThi.ShowPreview();
        }
        private void btnReSelect_Click(object sender, EventArgs e)
        {
            LoadData_Lue();
        }
        #endregion
        #region[Các hàm sữ dụng]
        private void LoadData_Lue()
        {
            try
            {
            m_iDpt = 0;
            m_iLel = 0;
            m_iMaj = 0;
            m_iCou = 0;
            m_iTty = 0;
            lueCatalogue_Course.EditValue = null;
            lueCatalogue_Department.EditValue = null;
            lueCatalogue_Level.EditValue = null;
            lueCatalogue_Major.EditValue = null;
            lueCatalogue_TrainingType.EditValue = null;
            dt = 
                (this.m_data.m_DCReadOnly.Training_Classes.GetDepartmentInTrainingClass(
                this.m_data.m_DCReadOnly.Catalogue_Departments)).ToDataTable();
            lueCatalogue_Department.GetData(dt, noChoose: true);
            dt = 
                (this.m_data.m_DCReadOnly.Training_Classes.GetLevelInTrainingClass(
                this.m_data.m_DCReadOnly.Catalogue_Levels)).ToDataTable();
            lueCatalogue_Level.GetData(dt, noChoose: true);
            dt = 
                (this.m_data.m_DCReadOnly.Training_Classes.GetMajorInTrainingClass(
                this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
            lueCatalogue_Major.GetData(dt, noChoose: true);
            dt = 
                (this.m_data.m_DCReadOnly.Training_Classes.GetTrainingTypeInTrainingClass(
                this.m_data.m_DCReadOnly.Catalogue_TrainingTypes)).ToDataTable();
            lueCatalogue_TrainingType.GetData(dt, "Catalogue_TrainingType", noChoose: true);
            dt = 
                (this.m_data.m_DCReadOnly.Training_Classes.GetCourseInTrainingClass(
                this.m_data.m_DCReadOnly.Catalogue_Courses)).ToDataTable();
            lueCatalogue_Course.GetData(dt, noChoose: true);
            }
            catch
            {
            }
        }
        private void ArrangeColume(DataTable dt,int m_iDpt,int m_iLel,int m_iMaj,int m_iTty,int m_iCou)
        {
            DataTable draf = new DataTable();
            for (int i = 0; i < dt.Columns.Count - 1; i++)
            {
                draf.Columns.Add();
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                draf.Rows.Add();
                draf.Rows[i][0] = "";
            }
            #region [Sắp Xếp Thứ Tự Các Cột]
            for (int i = 0; i < dt.Columns.Count - 1; i++)
            {
            aa:
                if (dt.Columns[i].ColumnName == "Catalogue_DepartmentName" && m_iDpt != 0)
                {
                    try
                    {

                        draf.Columns[m_iDpt - 1].ColumnName = dt.Columns[i].ColumnName.ToString();
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            draf.Rows[j][m_iDpt - 1] = dt.Rows[j][i].ToString();
                        }
                    }
                    catch
                    {
                        m_iDpt--;
                        goto aa;
                    }
                }
                else
                {
                bb:
                    if (dt.Columns[i].ColumnName == "Catalogue_LevelName" && m_iLel != 0)
                    {
                        try
                        {
                            draf.Columns[m_iLel - 1].ColumnName = dt.Columns[i].ColumnName.ToString();
                            for (int j = 0; j < dt.Rows.Count; j++)
                            {
                                draf.Rows[j][m_iLel - 1] = dt.Rows[j][i].ToString();
                            }
                        }
                        catch
                        {
                            m_iLel--;
                            goto bb;
                        }
                    }
                    else
                    {
                    cc:
                        if (dt.Columns[i].ColumnName == "Catalogue_MajorName" && m_iMaj != 0)
                        {
                            try
                            {
                                draf.Columns[m_iMaj - 1].ColumnName = dt.Columns[i].ColumnName.ToString();
                                for (int j = 0; j < dt.Rows.Count; j++)
                                {
                                    draf.Rows[j][m_iMaj - 1] = dt.Rows[j][i].ToString();
                                }
                            }
                            catch
                            {
                                m_iMaj--;
                                goto cc;
                            }
                        }
                        else
                        {
                        dd:
                            if (dt.Columns[i].ColumnName == "Catalogue_TrainingTypeName" && m_iTty != 0)
                            {
                                try
                                {
                                    draf.Columns[m_iTty - 1].ColumnName = dt.Columns[i].ColumnName.ToString();
                                    for (int j = 0; j < dt.Rows.Count; j++)
                                    {
                                        draf.Rows[j][m_iTty - 1] = dt.Rows[j][i].ToString();
                                    }
                                }
                                catch
                                {
                                    m_iTty--;
                                    goto dd;
                                }
                            }
                            else
                            {
                            ee:
                                if (dt.Columns[i].ColumnName == "Catalogue_CourseName" && m_iCou != 0)
                                {
                                    try
                                    {
                                        draf.Columns[m_iCou - 1].ColumnName = dt.Columns[i].ColumnName.ToString();
                                        for (int j = 0; j < dt.Rows.Count; j++)
                                        {
                                            draf.Rows[j][m_iCou - 1] = dt.Rows[j][i].ToString();
                                        }
                                    }
                                    catch
                                    {
                                        m_iCou--;
                                        goto ee;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            #region [Chép Dữ Liệu Vào Bảng]
            for (int i = 0; i < dt.Columns.Count - 1; i++)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    dt.Rows[j][i] = draf.Rows[j][i].ToString();
                }
            }
            #endregion            
        }
        private void CheckSelect()
        {
            if (lueCatalogue_Course.GetIntValue() < 0)
            {
                m_iCou = 0;
            }
            if (lueCatalogue_Department.GetIntValue() < 0)
            {
                m_iDpt = 0;
            }
            if (lueCatalogue_Level.GetIntValue() < 0)
            {
                m_iLel = 0;
            }
            if (lueCatalogue_Major.GetIntValue() < 0)
            {
                m_iMaj = 0;
            }
            if (lueCatalogue_TrainingType.GetIntValue() < 0)
            {
                m_iTty = 0;
            }
        }
        #endregion                
    }
}